/**
 * 
 */

package com.kugou.game.sdk.ui.activity;

import com.kugou.framework.v4.Fragment;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.ui.fragment.SecurityEmailBindFragment;
import com.kugou.game.sdk.ui.fragment.SecurityEmailModifyFragment;
import com.kugou.game.sdk.ui.fragment.SecurityEmailResultFragment;
import com.kugou.game.sdk.utils.Rx;

import android.os.Bundle;
import android.text.TextUtils;

/**
 * 描述:绑定(修改)安全邮箱
 *
 * @author jerryliu
 * @since 2015年8月19日 下午6:16:07
 */
public class BindSecurityEmailActivity extends BaseCommonTitleFragmentActivity {
    public static final String SHOW_BIND_SECURITY_EMAIL_RESULT = "showBindEmailResult";

    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = Rx.layout.kg_activity_sdk_common;
        setContentView(layoutId);
        user = UserManager.getInstance().getCurrentUserInternal();
        if (user == null) {
            showToast("请先登录");
            finish();
            return;
        }
        showPage();
    }

    private void showPage() {
        boolean showResult = getIntent().getBooleanExtra(SHOW_BIND_SECURITY_EMAIL_RESULT, false);
        if (showResult) {
            setCommonTitle("绑定邮箱");
            SecurityEmailResultFragment fragment = new SecurityEmailResultFragment();
            getSupportFragmentManager().beginTransaction().replace(Rx.id.kg_container, fragment)
                    .commitAllowingStateLoss();
        } else {
            if (!TextUtils.isEmpty(user.getSecurityEmail())) {
                setCommonTitle("修改邮箱");
                SecurityEmailModifyFragment fragment = new SecurityEmailModifyFragment();
                getSupportFragmentManager().beginTransaction()
                        .replace(Rx.id.kg_container, fragment).commitAllowingStateLoss();
            } else {
                setCommonTitle("绑定邮箱");
                SecurityEmailBindFragment fragment = new SecurityEmailBindFragment();
                getSupportFragmentManager().beginTransaction()
                        .replace(Rx.id.kg_container, fragment).commitAllowingStateLoss();
            }
        }
    }

    private void switchPage(String fragmentName, Bundle arguments) {
        try {
            if (!TextUtils.isEmpty(fragmentName)) {
                Fragment fragment = (Fragment) Class.forName(fragmentName).newInstance();
                fragment.setArguments(arguments);
                getSupportFragmentManager().beginTransaction()
                        .replace(Rx.id.kg_container, fragment).commitAllowingStateLoss();
                // getSupportFragmentManager().beginTransaction().addToBackStack(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
